package cn.cxq.learning.string_matching;

import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.Comparator;

public class BigestNumber {

    @Test
    public void test() {
        System.out.println(solve(new int[]{0,0}));
    }

    public String solve (int[] nums) {
        // write code here
        StringBuffer result = new StringBuffer();

        if (nums.length == 0) {
            return result.toString();
        }

        ArrayList<String> ss = new ArrayList<>();

        for (int i = 0; i < nums.length; i++) {
            ss.add("" + nums[i]);
        }

        ss.sort((o1, o2)->{
            int length = Math.min(o1.length(),o2.length());
            int res = o2.substring(0, length).compareTo(o1.substring(0, length));

            if (o1.length() == o2.length()) {
                return res;
            }

            return res != 0 ? res : o1.length() > o2.length() ? o1.charAt(o2.length()) - o1.charAt(o2.length() - 1) : o2.charAt(o1.length()) - o2.charAt(o1.length() - 1);
        });

        for (String s : ss) {
            result.append(s);
        }

        if (result.toString().matches("0+")) {
            return "0";
        } else {
            return result.toString();
        }
    }
}
